﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class MemberIntegralService
    {
        /// <summary>
        /// 查询用户积分
        /// </summary>
        /// <param name="id"></param>
        /// <param name="memberNo"></param>
        /// <returns></returns>
        public MemberIntegral GetMemberIntegral(long id, String memberNo)
        {
            MemberIntegral integral = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    integral = new MemberIntegral();
                    integral.Id = id;
                    integral.MemberNo = memberNo;
                    integral = new MemberIntegralDAO().GetMemberIntegral(conn, integral);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return integral;
        }

        /// <summary>
        /// 查询会员积分列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo"></param>
        /// <param name="memberno"></param>
        /// <param name="type"></param>
        /// <param name="transno"></param>
        /// <param name="issuccess"></param>
        /// <param name="adminUsername"></param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetMemberIntegralTable(int pageindex, int pagesize, String enterpriseNo, String memberno, int type, String transno, bool? issuccess,
            String adminUsername, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    MemberIntegral integral = new MemberIntegral();
                    integral.Pageindex = pageindex;
                    integral.Pagesize = pagesize;
                    integral.EnterpriseNo = enterpriseNo;
                    integral.MemberNo = memberno;
                    integral.Type = type;
                    integral.TransNo = transno;
                    integral.IsSuccess = issuccess;
                    integral.AdminUsername = adminUsername;
                    integral.Ascfield = ascfield;
                    integral.Descfield = descfield;
                    dt = new MemberIntegralDAO().GetMemberIntegralTable(conn, integral);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询会员积分记录数
        /// </summary>
        /// <param name="enterpriseNo"></param>
        /// <param name="memberno"></param>
        /// <param name="type"></param>
        /// <param name="transno"></param>
        /// <param name="issuccess"></param>
        /// <param name="adminUsername"></param>
        /// <returns></returns>
        public long GetMemberIntegralRecord(String enterpriseNo, String memberno, int type, String transno, bool? issuccess, String adminUsername)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    MemberIntegral integral = new MemberIntegral();
                    integral.EnterpriseNo = enterpriseNo;
                    integral.MemberNo = memberno;
                    integral.Type = type;
                    integral.TransNo = transno;
                    integral.IsSuccess = issuccess;
                    integral.AdminUsername = adminUsername;
                    record = new MemberIntegralDAO().GetMemberIntegralRecord(conn, integral);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入用户积分
        /// </summary>
        /// <param name="integral"></param>
        /// <returns></returns>
        public long InsertMemberIntegral(MemberIntegral integral)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new MemberIntegralDAO().InsertMemberIntegral(conn, integral);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 插入会员积分
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="integral"></param>
        /// <returns></returns>
        public long InsertMemberIntegral(DbConnection conn, MemberIntegral integral)
        {
            long id = 0;
            try
            {
                id = new MemberIntegralDAO().InsertMemberIntegral(conn, integral);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新用户积分
        /// </summary>
        /// <param name="integral"></param>
        /// <returns></returns>
        public int UpdateMemberIntegral(MemberIntegral integral)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new MemberIntegralDAO().UpdateMemberIntegral(conn, integral);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除会员积分信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteMemberIntegral(String ids)
        {
            int count = 0;
            if (!String.IsNullOrEmpty(ids))
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    MemberIntegralDAO integralDAO = new MemberIntegralDAO();
                    MemberIntegral integral = new MemberIntegral();
                    String[] idArr = ids.Split(new Char[] { ',' });
                    foreach (String lopid in idArr)
                    {
                        integral.Id = Int64.Parse(lopid);
                        count += integralDAO.DeleteMemberIntegral(conn, integral);
                    }
                }
            }
            return count;
        }
    }
}
